﻿angular.module('starter')
.controller("placeOrderCtrl", function ($scope, $location, $stateParams, $state, CountDown,
    appBase, authService, httpService, UserInfo) {

    //初始化
    initialize();
    $scope.placeOrder = function ()
    {
//      $state.go('paymentRoute', { reInfo: { orderId: "1000001", lockId: "100001", hisOrderId: "100001" } });
//
//      return;
        var parData = {};
        parData.hospitalCode=$scope.hspCode;
        parData.deptId=$scope.DoctorParma.deptId;
        parData.doctorId=$scope.DoctorParma.doctorId;
        parData.beginDate=$scope.DoctorParma.regTime;
        parData.endDate=$scope.DoctorParma.regTime;
        parData.regDate=$scope.DoctorParma.regDate;
        parData.scheduleFlag=$scope.DoctorParma.scheduleType;
        parData.patIdCard=$scope.UInfo.idCard;
        parData.patMedNo=$scope.UInfo.hisCardNumber;
        parData.patName=$scope.UInfo.DisplayName;
        parData.patMobile=$scope.UInfo.Tel;
        parData.operMobile=$scope.UInfo.Tel;
        parData.orderType = "APP";
        parData.fee =parseInt($scope.DoctorParma.regFee);
        parData.treatfee = parseInt($scope.DoctorParma.treatFee);
        parData.account_code = $scope.UInfo.UserName
        parData.appId = appBase.appId;
        parData.order_amount = (parseInt($scope.DoctorParma.treatFee) + parseInt($scope.DoctorParma.regFee));
        parData.order_expire_time = 15;//15分钟过期
         

        if ($scope.DoctorParma.type == 2) {
            //预约以后的
            httpService.post(httpService.appWebApi.addPreRegOrder,
                parData
                ).then(
                function (success) {
                    var data = success.data;
                    if (data.returnCode != "0") {
                        console.log(data.returnMsg)
                        return;
                    }
                    $state.go('paymentRoute', { reInfo: { orderId: data.returnData[0].orderId, lockId: "", hisOrderId: data.returnData[0].hisOrderId } });
                    return;
                }
                , function (error) {
                    console.log(error);
                });
        } else {
            //预约当天的
            httpService.post(httpService.appWebApi.lockRegSeqToday,
              parData
              ).then(
              function (success) {
                  var data = success.data;
                  if (data.returnCode != "0") {
                      console.log(data.returnMsg)
                      return;
                  }
                  $state.go('paymentRoute', { reInfo: { orderId: data.returnData[0].orderId, lockId: data.returnData[0].lockId, hisOrderId: "" } });
                  return;
              }
              , function (error) {
                  console.log(error);
              });
        }
    }

    //$scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
    //    //authService.isLogin();
    //    if (toState.url == "placeOrder" && fromState.url == "remarks") {
    //        if (toParams.tempData != null) {
    //            $scope.DoctorParma.remarks = toParams.tempData;
    //            console.log($scope.DoctorParma);
    //        }
    //        return true;
    //    }
    //    if (toState.url == "placeOrder" && (!authService.authentication.isAuth)) {
    //        //$state.go('userLogin');

    //        location.href = '/#/userLogin';
    //        return true;
    //    }
    //});
 
    //初始化
    function initialize()
    {
        CountDown.clear();
        $scope.hspName=appBase.hospitalName;
        $scope.hspCode=appBase.hospitalCode;
        $scope.hspaddr = appBase.addr;
        $scope.UInfo = UserInfo;
        $scope.DoctorParma = $stateParams.doctorParma || {};
        $scope.DoctorParma.scheduleType = $stateParams.scheduleType || "";
        $scope.DoctorParma.regDate = $stateParams.regDate || "";
        $scope.DoctorParma.regTime = $stateParams.regTime || "";
        $scope.DoctorParma.regFee = $stateParams.regFee || "";
        $scope.DoctorParma.tempData = $stateParams.tempData || [];
        $scope.DoctorParma.status = "NEW";//新建订单
        
        if ($stateParams.treatFee) {
            $scope.DoctorParma.treatFee = $stateParams.treatFee || "";
        }
        if ($stateParams.type)
        {
            $scope.DoctorParma.type = $stateParams.type || "1";
        }
        if($scope.DoctorParma.regDate == (new Date().Format("yyyy-MM-dd")))
        {
            $scope.DoctorParma.type = 1;
        }

        var orderData = {
            DoctorParma: $scope.DoctorParma,
            UserInfo: UserInfo,
            Hospital: {
                hspName: appBase.hospitalName,
                hspCode: appBase.hospitalCode,
                hspaddr: appBase.addr
            },
            remark: $scope.DoctorParma.tempData||{}
        }

        localStorage.setItem("orderData", angular.toJson(orderData));
     
        //需要登录
        if (!authService.authentication.isAuth) {
            $state.go('userLogin');
            //location.href = '/#/userLogin';
            return true;
        }
    }
 
})